package com.resource.transactions.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class SyncLogScheduler {

    /**
     * 全量索引
     */
    @Scheduled(cron = "0/30 * * * * *")
    public void fullImport() {
        log.info("----------------------------------------全量更新执行----------------------------------------");
        doUpdate("http://39.96.22.49:8080/solr8/new_core/dataimport?command=full-import&clean=true&commit=true");
    }


    /**
     * 增量索引
     */
    @Scheduled(cron = "0/20 * * * * *")
    public void deltaImport() {
        log.info("----------------------------------------增量更新执行----------------------------------------");
        doUpdate("http://39.96.22.49:8080/solr8/new_core/dataimport?command=delta-import&clean=false&commit=true");
    }


    private void doUpdate(String url) {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            //3.执行请求，获取响应
            response = client.execute(httpGet);
            //看请求是否成功，这儿打印的是http状态码
            log.info("--------------------更新状态码--------------------："+String.valueOf(response.getStatusLine().getStatusCode()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}